//
//  VisitiorLoginView.swift
//  HMWeibo007
//
//  Created by TTang on 15/6/29.
//  Copyright © 2015年 shanyu. All rights reserved.
//

import UIKit
import FFAutoLayout
//声明协议，定义要实现的方法
protocol VisitiorLoginViewDelegate:NSObjectProtocol {
   
    func registerButtonClicked()
    func loginButtonClicked()
 
}

class VisitiorLoginView: UIView {
    
    //声明代理属性
    weak var delegae: VisitiorLoginViewDelegate?
    
    
    
    //视图的默认构造方法
    //创建控件，并且制定布局，用xib & sb 开发不会调用，只有纯代码创建控件布局时调用
    override init(frame: CGRect) {
        super.init(frame: frame)
    
        setUI()
    }
    
    
    //解档，用xib & sb 开发会调用
    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    ///  设置访客视图信息
    ///
    ///  :param: message  文本标签内容
    ///  :param: iconName 图标名
    ///  :param: isHome   是否主页
    func setVisitiorViewInfo(message:String,iconName:String,isHome:Bool){
        messageLabel.text = message
        if isHome {
           homeIconView.hidden = false
          iconView.image = UIImage(named: "visitordiscover_feed_image_smallicon")
            
        }else{
            homeIconView.hidden = true
           iconView.image = UIImage(named: iconName)
            // 将视图层次置后
           sendSubviewToBack(maskIconView)
        }
    
    }
    
    ///定义动画启动方法，供首页视图调用
    func startAnimation(){
        
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = M_PI * 2
        anim.duration = 20
        anim.repeatCount = MAXFLOAT
 
        iconView.layer.addAnimation(anim, forKey: nil)
    }
    ///  用户注册
    func register(){
      
        delegae?.registerButtonClicked()
    }
    ///  用户登陆
    func login(){
    
        delegae?.loginButtonClicked()
    }
    ///  添加控件，设置布局
    func setUI(){
        //懒加载控件
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeIconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        //设置控件布局
        iconView.ff_AlignInner(ff_AlignType.CenterCenter, referView: self, size: nil, offset: CGPoint(x: 0, y: -40))
        homeIconView.ff_AlignInner(ff_AlignType.CenterCenter, referView: iconView, size: nil)
        messageLabel.ff_AlignVertical(ff_AlignType.BottomCenter, referView: iconView, size: CGSize(width: 240, height: 40), offset: CGPoint(x: 0, y: 20))
        registerButton.ff_AlignVertical(ff_AlignType.BottomLeft, referView: messageLabel, size: CGSize(width: 100, height: 40), offset: CGPoint(x: 0, y: 20))
        loginButton.ff_AlignVertical(ff_AlignType.BottomRight, referView: messageLabel, size: CGSize(width: 100, height: 40), offset: CGPoint(x: 0, y: 20))
        
        // 遮罩视图的布局
        maskIconView.ff_Fill(self, insets: UIEdgeInsets(top: 0, left: 0, bottom: 160, right: 0))

        
        backgroundColor = UIColor(white: 237.0/255.0, alpha: 1.0)
        
    }
    // MARK: - 懒加载控件
    /// 图标
    lazy var iconView:UIImageView = {
        
        let image = UIImage(named: "visitordiscover_feed_image_smallicon")
        return UIImageView(image: image)
    }()
    
    /// 遮罩图片，不要写 maskView,防止与系统冲突
    lazy var maskIconView:UIImageView = {
        
        let image = UIImage(named: "visitordiscover_feed_mask_smallicon")
    
        return UIImageView(image: image)
    }()
    /// 房子
    lazy var homeIconView : UIImageView = {
       
        let image = UIImage(named: "visitordiscover_feed_image_house")
        return UIImageView(image: image)
    }()
    
     /// 文字标签
    lazy var messageLabel : UILabel = {
        // 一旦使用了自动布局，frame 就失效了，在添加控件布局时有效
        let label = UILabel(frame: CGRectMake(220, 180, 240, 40))
        label.text = "登录后，最新、最热微博尽在掌握，不再会与实事潮流擦肩而过"
        label.textAlignment = NSTextAlignment.Center
        label.font = UIFont.systemFontOfSize(14)
        label.textColor = UIColor.darkGrayColor()
        label.preferredMaxLayoutWidth = 224
        label.numberOfLines = 2
        
        label.sizeToFit()
       
        return label
    }()
    /// 注册按钮
    lazy var registerButton : UIButton = {
    
        let btn = UIButton(frame: CGRectMake(110, 110, 100, 40))
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        btn.addTarget(self, action: "register", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
    }()
    
    /// 登录按钮
    lazy var loginButton : UIButton = {
       let btn = UIButton(frame: CGRectMake(200, 220, 100, 40))
       
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        btn.addTarget(self, action: "login", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    
    
}
